package com.android.clipps.ui;

import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.clipps.R;
import com.android.clipps.dao.ClippDao;
import com.android.clipps.dao.DaoMaster;
import com.android.clipps.dao.DaoSession;
import com.android.clipps.model.Clipp;
import com.android.clipps.uctoast.ListenClipboardService;
import com.hb.views.PinnedSectionListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.dao.query.Query;

public final class MainActivity extends AppCompatActivity {

    private final static String KEY_CONTENT = "content";
    private TextView mTextView;
    private PinnedSectionListView mListView;
    private String date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListenClipboardService.start(this);

        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
        DaoMaster daoMaster = new DaoMaster(helper.getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        ClippDao clippDao = daoSession.getClippDao();
        List<Clipp> dates = clippDao.loadAll();
        Map<String, List<Clipp>> map = new HashMap<>();
        for (Clipp clipp : dates) {
            if (map.containsKey(clipp.getDate())) {
                map.get(clipp.getDate()).add(clipp);
            } else {
                List<Clipp> list = new ArrayList<>();
                list.add(clipp);
                map.put(clipp.getDate(), list);
            }
        }

        List<Clipp> clipps = new ArrayList<>();
        for (String key : map.keySet()) {
            Clipp clippDate = new Clipp();
            clippDate.setContent(key);
            clippDate.setType(Clipp.TYPE_SECTION);
            clipps.add(clippDate);
            List<Clipp> list = map.get(key);
            for (Clipp clipp : list) {
                clipp.setType(Clipp.TYPE_ITEM);
                clipps.add(clipp);
            }
        }


        mListView = (PinnedSectionListView) findViewById(R.id.list);
        mListView.setShadowVisible(true);
        MyAdapter adapter = new MyAdapter(clipps);

        mListView.setAdapter(adapter);

    }


    class MyAdapter extends BaseAdapter implements PinnedSectionListView.PinnedSectionListAdapter{
        private List<Clipp> clipps;
        public MyAdapter(List<Clipp> clipps) {
            if (clipps != null) {
                this.clipps = clipps;
            } else {
                this.clipps = new ArrayList<>();
            }
        }
        @Override
        public int getCount() {
            return clipps.size();
        }

        @Override
        public Object getItem(int i) {
            return clipps.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public int getItemViewType(int position) {
            return clipps.get(position).getType();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Clipp clipp = clipps.get(i);
            if (clipp.getType() == Clipp.TYPE_ITEM) {
                view = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item, null, false);
            } else {
                view = LayoutInflater.from(MainActivity.this).inflate(R.layout.title_view, null, false);
            }
            TextView content = (TextView) view.findViewById(R.id.content);
            content.setText(clipp.getContent());
            return view;
        }

        /**
         * This method shall return 'true' if views of given type has to be pinned.
         *
         * @param viewType
         */
        @Override
        public boolean isItemViewTypePinned(int viewType) {
            return viewType == Clipp.TYPE_SECTION;
        }
    }
}